<?php namespace App\Http\Controllers\Api;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Http\Requests;
use App\Http\Services\UserManager;
use App\Http\Services\FileManager;
use App\User;
use App\Position;
use App\PersonalAuth;
use App\UserFollow;
use App\Attention;
use App\CompanyInfo;
use App\Resume;
use App\Userinfo;

class CertController extends Controller
{
    public function setPersonalAuth(Request $request){
        $idCardNum     = $request->get('idcard_num');
        $realName      = $request->get('realname');
        $idCardFrontId = $request->get('idcard_front_id');
        $idCardBackId  = $request->get('idcard_back_id');

        /** @var User $user */
        $user = UserManager::isLogin($request);
        if(!$user)
            return response()->json(['level' => 'error','message' => '请先登录!','data' => 'no_login']);

        $userInfo = Userinfo::whereUserId($user->id)->first();
        if(!$userInfo)
            return response()->json(['level' => 'error','message' => '用户信息不完整!']);

        if(is_null($idCardNum) || $idCardNum == '')
            return response()->json(["level"=>"error","message"=>"身份证号不能为空"]);

        if(is_null($realName) || $realName == '')
            return response()->json(["level"=>"error","message"=>"真实姓名不能为空"]);

        if(is_null($idCardFrontId) || $idCardFrontId == '')
            return response()->json(["level"=>"error","message"=>"身份证正面未上传"]);

        if(is_null($idCardBackId) || $idCardBackId == '')
            return response()->json(["level"=>"error","message"=>"身份证背面未上传"]);

        if($userInfo->personal_auth == 2){
            return response()->json(['level' => 'error','message' => '已在认证中，请勿修改']);
        }

        $personalAuth = PersonalAuth::whereUserId($user->id)->first();

        if(!$personalAuth){
            $personalAuth = new PersonalAuth();
            $personalAuth->user_id = $user->id;
        }

        if(!is_null($idCardNum))$personalAuth->idcard_num = $idCardNum;
        if(!is_null($realName))$personalAuth->realname = $realName;
        if(!is_null($idCardFrontId))$personalAuth->idcard_front_id = $idCardFrontId;
        if(!is_null($idCardBackId))$personalAuth->idcard_back_id = $idCardBackId;
        $personalAuth->save();

        $userInfo->personal_auth = 2;
        $userInfo->save();

        return response()->json(['level' => 'success','message' => '提交认证信息成功','data' => $personalAuth]);
    }

    public function getPersonalAuth(Request $request){
        $user = UserManager::isLogin($request);
        if(!$user)
            return response()->json(['level' => 'error','message' => '请先登录!','data' => 'no_login']);

        /** @var PersonalAuth $personalAuth */
        $personalAuth = PersonalAuth::whereUserId($user->id)->first();
        if(!$personalAuth)
            return response()->json(['level' => 'error','message' => '认证信息不存在']);

        $status = UserInfo::whereUserId($user->id)->first();
        $personalAuth->status           = $status->personal_auth;
        $personalAuth->idcard_front_url = FileManager::getImgFromOssUrl($personalAuth->idcard_front_id);
        $personalAuth->idcard_back_url  = FileManager::getImgFromOssUrl($personalAuth->idcard_back_id);

        return response()->json(['level' => 'success','message' => '获取成功','data' => $personalAuth]);
    }

    public function judCertDeta(Request $request){
        $user = UserManager::isLogin($request);
        if(!$user)
            return response()->json(['level' => 'error','message' => '请先登录!','data' => 'no_login']);

        $certDeta = array();
        $userInfo = UserInfo::whereUserId($user->id)->first();
        if(!$userInfo)
            return response()->json(['level' => 'error','message' => '个人资料未填写']);

        $resume_auth = 0;
        $resume = Resume::whereUserId($user->id)->first();
        if($resume)
            $resume_auth = 1;

        $position = Position::whereUserId($user->id)->count();

        $positionStatus = Position::whereUserId($user->id)->whereState(1)->count();

        $Attention = UserFollow::whereFUserId($user->id)->count();

        $UserAttention = UserFollow::whereUserId($user->id)->count();

        $company = 0;
        $companyInfo = CompanyInfo::whereUserId($user->id)->first();
        if($companyInfo)
            $company = 1;

        $certDeta['personal_auth'] = $userInfo->personal_auth;
        $certDeta['company_auth'] = $userInfo->company_auth;
        $certDeta['resumes'] = $resume_auth;
        $certDeta['position_num'] = $position;
        $certDeta['position_state_num'] = $positionStatus;
        $certDeta['company_info'] = $company;
        $certDeta['company_attention'] = $Attention;
        $certDeta['user_attention'] = $UserAttention;


        return response()->json(['level' => 'success','message' => '获取成功','data' => $certDeta]);
    }
}
